//
//  PrayerApi.m
//  Api
//
//  Created by CPX on 2018/5/29.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "PrayerApi.h"

/**
 祷告页接口
 @author yzl
 */
@implementation PrayerApi

+ (void)getCityForLatitude:(double)latitude longtitude:(double)longtitude success:(Success)success failure:(Failure)failure{
    NSString *url = @"http://maps.google.com/maps/api/geocode/json";
    NSString *latlng = [NSString stringWithFormat:@"%f,%f",latitude,longtitude];
    NSString *local;
    if([NSLocale preferredLanguages].count>0){
        local = [NSLocale preferredLanguages][0];
    }
    else{
        local = @"en-US";
    }
    [NetworkManager GETWithURL:url params:@{@"latlng":latlng,@"language":local,@"sensor":@"true"} success:^(id response) {
        
        if (response && [response[@"status"] isEqualToString:@"OK"]) {
            if ([response[@"results"] count]) {
                NSString *addr = response[@"results"][0][@"formatted_address"];
                if (addr.length) {
                    success(addr);
                }
            }
        }
        failure(nil);
        
    } failure:^(NSError *error) {
        failure(nil);
    }];
}

+ (void)getCityForLatitude:(double)latitude longitude:(double)longitude success:(Success)success failure:(Failure)failure{
    NSString *url = @"https://nominatim.openstreetmap.org/reverse";
//    NSString *latlng = [NSString stringWithFormat:@"%f,%f",latitude,longitude];
    NSString *local;
    if([NSLocale preferredLanguages].count>0){
        local = [NSLocale preferredLanguages][0];
    }
    else{
        local = @"en-US";
    }
    [NetworkManager GETWithURL:url params:@{@"lat":@(latitude),@"lon":@(longitude),@"accept-language":local,@"format":@"json2"} success:^(id response) {
        
        
        failure(nil);
        
    } failure:^(NSError *error) {
        failure(nil);
    }];
}

+ (void)getLocationForCity:(NSString *)city success:(Success)success failure:(Failure)failure{
    NSString *url = @"http://maps.google.com/maps/api/geocode/json";
    
    NSString *local;
    if([NSLocale preferredLanguages].count>0){
        local = [NSLocale preferredLanguages][0];
    }
    else{
        local = @"en-US";
    }
    [NetworkManager GETWithURL:url params:@{@"address":city,@"language":local} success:^(id response) {
        if (response && [response[@"status"] isEqualToString:@"OK"] && [response[@"results"] count]) {
            id data = response[@"results"][0];
            
            if (data[@"formatted_address"] && data[@"geometry"] && data[@"geometry"][@"location"]) {
                //json
                //"location" : {
                //                "lat" : 30.572815,
                //                "lng" : 104.066801
                success(data);
                
            }
            else{
                failure(nil);
            }
        }
        else{
            failure(nil);
        }
    
        
    } failure:^(NSError *error) {
        failure(nil);
    }];
}

+ (void)getWallpapersWithSuccess:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,IndexWallpaperPath];
    [NetworkManagerV3 GETWithURL:url header:[self header] params:nil success:^(id response) {
        if([self dealWithResponse:response]){
            success(response[@"data"]);
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        failure(error);
    } name:NSStringFromSelector(_cmd)];
}

+ (void)getIndexReommendListWithLimit:(NSInteger)limit offset:(NSInteger)offset success:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,IndexRecommendPath];
    [NetworkManagerV3 GETWithURL:url header:[self header] params:@{@"limit":@(limit),@"offset":@(offset)} success:^(id response) {
        if([self dealWithResponse:response]){
            success(response[@"data"]);
        }
        else{
            NSError *error = [NSError errorWithDomain:APPResponseError code:10001 userInfo:response];
            failure(error);
        }
    } failure:^(NSError *error) {
        failure(error);
    } name:NSStringFromSelector(_cmd)];
}

+ (void)getCompassSkinListWithSuccess:(Success)success failure:(Failure)failure{
    NSString *url = [NSString stringWithFormat:@"%@%@",RootURL,CompassSkinPath];
    [NetworkManagerV3 GETWithURL:url header:[self header] params:nil success:^(id response) {
        if ([self dealWithResponse:response]) {
            success(response[@"data"]);
        }
        else{
            failure(response);
        }
    } failure:^(NSError *error) {
        failure(error);
    } name:NSStringFromSelector(_cmd)];
}

@end
